package service

import (
	pb "gitee.com/shuokeyun/order-service/api/v1"
	"gitee.com/shuokeyun/order-service/internal/domain"
	"github.com/jinzhu/copier"
	"time"
)

func createOrderRequestConvDo(req *pb.CreateOrderRequest) *domain.Order {
	if req == nil {
		return nil
	}
	var payTime *time.Time
	if req.PayTime.IsValid() {
		v := req.PayTime.AsTime().Local()
		payTime = &v
	}
	return &domain.Order{
		AppId:          req.AppId,
		Status:         int8(req.Status),
		SettleType:     int8(req.SettleType),
		PriceArea:      req.PriceArea,
		Brand:          req.Brand,
		TotalPrice:     req.TotalPrice,
		OrderPrice:     req.OrderPrice,
		MerchantId:     req.MerchantId,
		MerchantUserId: req.MerchantUserId,
		PayStatus:      int8(req.PayStatus),
		PayMethod:      int8(req.PayMethod),
		PayTime:        payTime,
		SalesmanId:     req.SalesmanId,
		Ext:            orderExtConvDo(req.Ext),
		Barcodes:       orderBarcodeCreateConvDo(req.Barcodes),
		Originals:      orderOriginalCreateConvDo(req.Originals),
		Users:          orderUserCreateConvDo(req.Users),
	}
}

func orderExtConvDo(ext *pb.OrderExt) *domain.OrderExt {
	if ext == nil {
		return nil
	}
	return &domain.OrderExt{
		Remark:            ext.Remark,
		Department:        ext.Department,
		OutpatientNo:      ext.OutpatientNo,
		BedNo:             ext.BedNo,
		ClinicalDiagnosis: ext.ClinicalDiagnosis,
	}
}

func orderBarcodeCreateConvDo(barcodes []*pb.OrderBarcodeCreate) []*domain.OrderBarcode {
	if len(barcodes) == 0 {
		return nil
	}
	list := make([]*domain.OrderBarcode, len(barcodes))
	for k, barcode := range barcodes {
		list[k] = &domain.OrderBarcode{
			Barcode:    barcode.Barcode,
			Consumable: barcode.Consumable,
			Number:     int8(barcode.Number),
			Status:     int8(barcode.Status),
			Items:      orderItemCreateConvDo(barcode.Items),
		}
		if barcode.SamplingTime.IsValid() {
			v := barcode.SamplingTime.AsTime().Local()
			list[k].SamplingTime = &v
		}
	}
	return list
}

func orderItemCreateConvDo(items []*pb.OrderItemCreate) []*domain.OrderItem {
	if len(items) == 0 {
		return nil
	}
	list := make([]*domain.OrderItem, len(items))
	for k, item := range items {
		list[k] = &domain.OrderItem{
			ItemCode:    item.ItemCode,
			ItemName:    item.ItemName,
			Price:       item.Price,
			MarketPrice: item.MarketPrice,
		}
	}
	return list
}

func orderOriginalCreateConvDo(originals []*pb.OrderOriginalCreate) []*domain.OrderOriginal {
	if len(originals) == 0 {
		return nil
	}
	list := make([]*domain.OrderOriginal, len(originals))
	for k, original := range originals {
		list[k] = &domain.OrderOriginal{
			ItemCode:       original.ItemCode,
			ItemName:       original.ItemName,
			ItemCodeDetail: original.ItemCodeDetail,
			Price:          original.Price,
			MarketPrice:    original.MarketPrice,
		}
	}
	return list
}

func orderUserCreateConvDo(users []*pb.OrderUserCreate) []*domain.OrderUser {
	if len(users) == 0 {
		return nil
	}
	list := make([]*domain.OrderUser, len(users))
	for k, user := range users {
		list[k] = &domain.OrderUser{
			UserId:   user.UserId,
			Name:     user.Name,
			Age:      int16(user.Age),
			Sex:      int8(user.Sex),
			Mobile:   user.Mobile,
			CardType: int8(user.CardType),
			CardId:   user.CardId,
		}
	}
	return list
}

// copier:auto
func orderConvOrderReply(do *domain.Order) *pb.GetOrderReply {
	var reply pb.GetOrderReply
	copier.Copy(&reply, do)
	return &reply
}
